package com.timk.goserver.client.ui.screens;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.timk.goserver.client.model.Message;
import com.timk.goserver.client.services.NoSessionException;
import com.timk.goserver.client.ui.widgets.DisplayUserLinkListener;
import com.timk.goserver.client.ui.widgets.StatusLabel;
import com.timk.goserver.client.ui.widgets.UserLink;

/**
 * Panel for displaying messages
 * @author TKington
 *
 */
public class MessagePanel extends Composite {
	final StatusLabel status; // NOPMD - Explicit scoping
	private final FlexTable table;
	private final DisplayUserLinkListener userLinkListener;
	
	/**
	 * Creates a MessagePanel
	 * @param mainPanel the main panel
	 */
	public MessagePanel(MainPanel mainPanel) {
		this.userLinkListener = new DisplayUserLinkListener(mainPanel);
		
		VerticalPanel vertPanel = new VerticalPanel();
		
		status = new StatusLabel();
		vertPanel.add(status);
		
		table = new FlexTable();
		table.addStyleName("goserver-Table");
		vertPanel.add(table);
		
		initWidget(vertPanel);
		
		refreshMessages();
	}
	
	final void refreshMessages() {
		MainPanel.msgService.getMessages(new AsyncCallback() {
			public void onSuccess(Object result) {
				status.clear();
				fillTable((List)result);
			}
			
			public void onFailure(Throwable caught) {
				if(caught instanceof NoSessionException) {
					MainPanel.sessionExpired(caught.getMessage());
					return;
				}
				
				status.setError("Couldn't retrieve messages");
			}
		});
	}
	
	final void fillTable(List messages) {
		if(messages.isEmpty()) {
			table.setVisible(false);
			return;
		}
		
		table.setVisible(true);
		
		int numRows = table.getRowCount();
		for(int i = 0; i < numRows; i++) {
			table.removeRow(0);
		}
		
		table.setHTML(0, 0, "<b>From</b>");
		table.setHTML(0, 1, "<b>Subject</b>");
		table.setHTML(0, 2, "<b>Date</b>");
		
		int curRow = 1;
		for(int i = 0; i < messages.size(); i++) {
			Message message = (Message)messages.get(i);

			String fromUser = message.getFromUser();
			if("System".equals(fromUser)) {
				table.setText(curRow, 0, "System");
			} else {
				UserLink userLink = new UserLink(null, fromUser, userLinkListener);
				table.setWidget(curRow, 0, userLink);
			}
			
			table.setText(curRow, 1, message.getSubject());
			table.setText(curRow, 2, message.getDate().toString());
			
			Button acceptButton = new Button("Read");
			acceptButton.addClickListener(new ReadListener(message));
			table.setWidget(curRow, 3, acceptButton);
			
			curRow++;
		}
	}
	
	class ReadListener implements ClickListener {
		private final Message message;
		
		ReadListener(Message message) {
			this.message = message;
		}
		
		public void onClick(Widget sender) {
			MessageDialog dlg = new MessageDialog(message, false);
			dlg.show();
			
			dlg.addPopupListener(new PopupListener() {
				public void onPopupClosed(PopupPanel popup, boolean autoClosed) {					
					refreshMessages();
				}
			});
		}
	}
}
